﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using Core.Model.Column;
using Core.Model.Hierarchy;
using UI.Resources.Controls.Columns;

namespace UI.Resources.Controls
{
    public partial class SubstitutableDataGrid
    {
        public SubstitutableDataGrid()
        {
            InitializeComponent();
        }

        #region Public fields

        public UIElement SubstitutionTemplate
        {
            get { return (UIElement) GetValue(SubstitutionTemplateProperty); }
            set { SetValue(SubstitutionTemplateProperty, value); }
        }

        public Research Research
        {
            get { return (Research)GetValue(ResearchProperty); }
            set { SetValue(ResearchProperty, value); }
        }

	    public bool IdAutogeneration
	    {
			get { return (bool)GetValue(IdAutogenerationProperty); }
			set { SetValue(IdAutogenerationProperty, value); }
	    }

	    public IEnumerable<string> Keywords { get; private set; }

        #endregion

        #region Dependency properties

        public static readonly DependencyProperty SubstitutionTemplateProperty = DependencyProperty.Register("SubstitutionTemplate", typeof(UIElement), typeof(SubstitutableDataGrid));

        public static readonly DependencyProperty ResearchProperty = DependencyProperty.Register("Research", typeof(Research), typeof(SubstitutableDataGrid));

		public static readonly DependencyProperty IdAutogenerationProperty = DependencyProperty.Register("IdAutogeneration", typeof(bool), typeof(SubstitutableDataGrid));

        #endregion

		#region Public methods

		public void SetKeywords(IEnumerable<string> keywords)
		{
			Keywords = keywords;
		}

		public void SetIdAutogeneration(bool isIdAutogenerating)
		{
			IdAutogeneration = isIdAutogenerating;
		}

		public void ResetColumns(bool clearColumns)
		{
			CasesDataGrid.AutoGenerateColumns = false;
			if (clearColumns)
			{
				CasesDataGrid.Columns.Clear();
			}
			CasesDataGrid.AutoGenerateColumns = true;
		}

		#endregion

		#region Event handlers

		private void DataGridAutoGeneratedColumns(object sender, EventArgs e)
	    {
		    var control = ((ItemsControl) sender);

			if (control.Items != null)
			{
				var dataGrid = (DataGrid) sender;
				dataGrid.Columns.Clear();

				var columnsList = Research.Columns.ToArray();
				foreach (ExaminationColumn column in columnsList)
				{
					DataGridColumn dataGridColumn;
					switch (column.Type)
					{
						case ColumnType.Identifier:
							dataGridColumn = new DataGridTextColumn
								                 {
													 Header = column.Name,
													 Binding = new Binding(column.Name){ Mode = BindingMode.TwoWay },
													 IsReadOnly = IdAutogeneration
								                 };
							break;
						case ColumnType.Keyword:
							dataGridColumn = new DataGridComboBoxSelectorColumn(
								column.Name,
								new Binding(column.Name) { Mode = BindingMode.TwoWay},
								new Binding("Keywords") { ElementName = "This" });
							break;
						default:
							dataGridColumn = new DataGridTextColumn
												 {
													 Header = column.Name,
													 Binding = new Binding(column.Name) { Mode = BindingMode.TwoWay }
												 };	
							break;
					}
					dataGrid.Columns.Add(dataGridColumn);
				}
			}
		}

		private void DataGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
			Research.IsDirty = true;
		}

		#endregion

	    private void DataGridInitializingNewItem(object sender, InitializingNewItemEventArgs e)
	    {
		    if (IdAutogeneration)
		    {
			    foreach (ExaminationColumn column in Research.Columns)
			    {
				    var row = (IDictionary<string, object>) e.NewItem;
				    if (column.Type == ColumnType.Identifier)
				    {
					    row[column.Name] = Research.GenerateNextId();
				    }
			    }
		    }
	    }
    }
}